package com.kitty.action;

import java.util.Map;

import com.kitty.model.TUser;
import com.kitty.service.IUserMgr;
import com.kitty.util.LoginInterceptor;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1619786962753557599L;
	
	private String email;
	private String pwd;
	private String valCode;
	
	private IUserMgr serviceUser;

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getValCode() {
		return valCode;
	}

	public void setValCode(String valCode) {
		this.valCode = valCode;
	}
	
	public void setServiceUser(IUserMgr serviceUser) {
		this.serviceUser = serviceUser;
	}
	
	
	@Override
	public String execute() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
		String valCode2 = (String)session.get("valCode");
		session.put("valCode", null);
		
		if (valCode2.toLowerCase().equals(getValCode().toLowerCase())) {
			TUser user = serviceUser.checkLogin(getEmail(), getPwd());
			if (user != null) {
				session.put(LoginInterceptor.USER_SESSION_KEY, user);
				return SUCCESS;
				
			} else {
				addActionError("Email或密码不匹配");
			}
			
		} else {
			addActionError("验证码不匹配，请重新输入");
		}
		
		return INPUT;
	}

	
}
